package com.testing.hibernateapp.service;

import java.io.Serializable;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;

public class UniversalHibernateDAOImpl<T> implements UniversalHibernateDAO<T> {

	private Session session;
	private Class objectClass;

	public UniversalHibernateDAOImpl(Session session) {
		this.session = session;
	}
	
	public Collection<T> retrieve() {
		Criteria criteria = session.createCriteria(objectClass);
		return  criteria.list();
	}
	
	@SuppressWarnings("unchecked")
	public T retrieve(Serializable id) {
		return (T) session.get(objectClass, id);
	}
	
	@SuppressWarnings("unchecked")
	public T save(T t) {
		return (T) session.get(objectClass, session.save(t));
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public Class getObjectClass() {
		return objectClass;
	}

	public void setObjectClass(Class objectClass) {
		this.objectClass = objectClass;
	}

}
